let haystack = "leetcode", needle = "leeto"
var strStr = function (haystack, needle) {
    if (needle.length === 0) return 0; // 边界情况：needle为空
    if (needle.length > haystack.length) return -1;

    const len = haystack.length;
    const needleLen = needle.length;
    let i = 0;

    while (i <= len - needleLen) { // 只需检查可能匹配的起始位置
        if (haystack[i] === needle[0]) {
            let match = true;
            for (let j = 1; j < needleLen; j++) {
                if (haystack[i + j] !== needle[j]) {
                    match = false;
                    break;
                }
            }
            if (match) return i; // 完全匹配时才返回
        }
        i++; // 无论是否部分匹配，每次只递增1
    }
    return -1;
};
console.log(strStr(haystack, needle));
